package meiyunting.shiwuzhaoling.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 健康检查控制器
 * 用于Consul服务健康检查
 */
@RestController
public class HealthCheckController {

    @Value("${spring.application.name:lost-found-service}")
    private String applicationName;

    @Value("${server.port:8081}")
    private int serverPort;

    /**
     * 自定义健康检查端点
     * @return 健康状态信息
     */
    @GetMapping("/api/health")
    public Map<String, Object> healthCheck() {
        Map<String, Object> health = new HashMap<>();
        health.put("status", "UP");
        health.put("service", applicationName);
        health.put("port", serverPort);
        health.put("timestamp", System.currentTimeMillis());
        
        System.out.println("【健康检查】API健康检查请求 - 端口: " + serverPort);
        
        return health;
    }
    
    /**
     * Actuator健康检查端点
     * 与Consul配置中的health-check-path匹配
     * @return 健康状态信息
     */
    @GetMapping("/actuator/health")
    public Map<String, Object> actuatorHealth() {
        Map<String, Object> health = new HashMap<>();
        health.put("status", "UP");
        
        // 创建components对象
        Map<String, Object> components = new HashMap<>();
        
        // 创建各个组件状态
        Map<String, String> diskSpace = new HashMap<>();
        diskSpace.put("status", "UP");
        
        Map<String, String> ping = new HashMap<>();
        ping.put("status", "UP");
        
        Map<String, String> db = new HashMap<>();
        db.put("status", "UP");
        
        // 将组件状态添加到components中
        components.put("diskSpace", diskSpace);
        components.put("ping", ping);
        components.put("db", db);
        
        // 将components添加到health中
        health.put("components", components);
        
        System.out.println("【健康检查】Actuator健康检查请求 - 端口: " + serverPort);
        
        return health;
    }
} 